﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class UIBGA : ViewBase
    {
       
        public VBASTGA TMV
        {
            get
            {
                return (VBASTGA)MV;
            }
        }
        #region ==YARN===========
        public static readonly DependencyProperty YARNProperty = DependencyProperty.Register("YARN",
            typeof(string), typeof(UIBGA),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnYARNChanged));
        public string YARN
        {
            get
            {
                return (string)GetValue(YARNProperty);
            }
            set
            {
                SetValue(YARNProperty, value);
                OnYARNChg();
            }
        }
        private static void OnYARNChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIBGA;
            if (src != null)
            {
                src.YARN = (string)e.NewValue;
            }
        }
       
        #endregion
        #region ==WTKEY===========
        public static readonly DependencyProperty WTKEYProperty = DependencyProperty.Register("WTKEY",
        typeof(string), typeof(UIBGA),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWTKEYChanged));
        public string WTKEY
        {
            get
            {
                return (string)GetValue(WTKEYProperty);
            }
            set
            {
                SetValue(WTKEYProperty, value);
                OnWTKEYChg();
            }
        }
        private static void OnWTKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIBGA;
            if (src != null)
            {
                src.WTKEY = (string)e.NewValue;
            }
        }
        public virtual void OnWTKEYChg()
        { }
        #endregion

        #region ==FAKTOR===========
        public static readonly DependencyProperty FAKTORProperty = DependencyProperty.Register("FAKTOR",
        typeof(double), typeof(UIBGA),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFAKTORChanged));
        public double FAKTOR
        {
            get
            {
                return (double)GetValue(FAKTORProperty);
            }
            set
            {
                SetValue(FAKTORProperty, value);
                OnFAKTORChg();
            }
        }
        private static void OnFAKTORChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIBGA;
            if (src != null)
            {
                src.FAKTOR = (double)e.NewValue;
            }
        }
        public virtual void OnFAKTORChg()
        { }
        #endregion

        #region ==IsFromWT===========
        public static readonly DependencyProperty IsFromWTProperty = DependencyProperty.Register("IsFromWT",
        typeof(bool), typeof(UIBGA),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFromWTChanged));
        public bool IsFromWT
        {
            get
            {
                return (bool)GetValue(IsFromWTProperty);
            }
            set
            {
                SetValue(IsFromWTProperty, value);
            }
        }
        private static void OnIsFromWTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIBGA;
            if (src != null)
            {
                src.IsFromWT = (bool)e.NewValue;
            }
        }
        #endregion 

        #region ==WARPENDS===========
        public static readonly DependencyProperty WARPENDSProperty = DependencyProperty.Register("WARPENDS",
        typeof(double), typeof(UIBGA),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWARPENDSChanged));
        public double WARPENDS
        {
            get
            {
                return (double)GetValue(WARPENDSProperty);
            }
            set
            {
                SetValue(WARPENDSProperty, value);

            }
        }
        private static void OnWARPENDSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIBGA;
            if (src != null)
            {
                src.WARPENDS = (double)e.NewValue;
                src.OnWARPENDSChg((double)e.NewValue);
            }
        }
        public virtual void OnWARPENDSChg(double value)
        {

        }
        #endregion

        #region ==WEIGHT===========
        public static readonly DependencyProperty WEIGHTProperty = DependencyProperty.Register("WEIGHT",
        typeof(double), typeof(UIBGA),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWEIGHTChanged));
        public double WEIGHT
        {
            get
            {
                return (double)GetValue(WEIGHTProperty);
            }
            set
            {
                SetValue(WEIGHTProperty, value);

            }
        }
        private static void OnWEIGHTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIBGA;
            if (src != null)
            {
                src.WEIGHT = (double)e.NewValue;
                src.OnWEIGHTChg((double)e.NewValue);
            }
        }
        public virtual void OnWEIGHTChg(double value)
        {

        }
        #endregion

        public UIBGA()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
        }
        public int pos = 0;
       
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);

            if (dwMaster == null)
            {
                KEY = "";
                YARN = "";
            }
            else
            {
                KEY = "B"+TMV.BAGART + TMV.BAGVAR + TMV.BAGFARBE+TMV.BAGPOS;
                YARN ="G"+ TMV.BAGGART + TMV.BAGGVAR + TMV.BAGGFARBE;
            }
            if (dwMaster != null)
            {

                filter = "BAGART='" + dwMaster["BAGART"].ToString() + "'";
                sort = "BAGPOS DESC";
                rows = TBMain.Select(filter, sort);
                foreach (DataRow row in rows)
                {
                    pos = int.Parse(row["BAGPOS"].ToString());
                    break;
                }
            }
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (TBMain.Rows.Count == 1)
                return;
            TMV.DBG01(dwMaster);
            filter = "BAGPOS='" + dwMaster["BAGPOS"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
                row.Delete();
            vwMaster.View.MoveCurrentToFirst();
            base.DeleteCommand(sender, e);
            TBMain.AcceptChanges();
            Validate();
        }
        string BEAMART = "";
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (BEAMART == "")
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["BAGART"] = BEAMART.Substring(1, 20);
            dwData["BAGVAR"] = BEAMART.Substring(21, 2);
            dwData["BAGFARBE"] = BEAMART.Substring(23, 10);
            pos++;
            dwData["BAGPOS"] = Helper.Right("00" + pos, 2); 
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            TMV.InsertBASTGA(dwData);
            base.NewCommand(sender, e);
        }
        public override void OnEXAUFChg(string vlaue)
        {
            pos = 0;
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (EXAUF.Length < 32)
                return;
            PM["ART"] = EXAUF.Substring(1, 20);
            PM["VAR"] = EXAUF.Substring(21, 2);
            PM["FARBE"] = EXAUF.Substring(23, 10);
            BEAMART = EXAUF.Substring(0, 33);
            SResult = TMV.LOADBYWS(PM);
            MergeData(SResult, true);
            WEIGHT = Helper.RoundX(TMV.SUM1 / 100, 1) * 100;
            if (!IsAuto)
                return;
            if (Counts > 0)
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["BAGMANDANT"] = app.Mandant;
            dwData["BAGART"] = EXAUF.Substring(1, 20);
            dwData["BAGVAR"] = EXAUF.Substring(21, 2);
            dwData["BAGFARBE"] = EXAUF.Substring(23, 10);
            dwData["BAGPOS"] = "01";
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            TMV.InsertBASTGA(dwData); 
            base.OnEXAUFChg(vlaue);
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (!Validate())
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                TMV.UpdateBASTGA(row, app.company, 0);
            }
            base.OnSave();
        }
        public void RefreshCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsEdit)
                    return;
                if (WTKEY.Trim().Length == 0)
                    return;
                if (BEAMART.Length < 32)
                    return;

                dwData = TBMain.NewRow();
                dwData["BAGMANDANT"] = app.Mandant;
                dwData["BAGART"] = BEAMART.Substring(1, 20);
                dwData["BAGVAR"] = BEAMART.Substring(21, 2);
                dwData["BAGFARBE"] = BEAMART.Substring(23, 10);
                TMV.DBG02(dwData);
                TBMain.Clear();
                PM["SRFNR"] = WTKEY.Trim().PadRight(15);
                SResult = TMV.VWT.LOADBYBS(PM);
                if (SResult.Tables[0].Rows.Count == 0)
                    return;
                DataRow dwWT = SResult.Tables[0].Rows[0];
                for (int YNi = 1; YNi < 100; YNi++)
                {
                    string yarn = dwWT["WTBGARN" + YNi].ToString();
                    double ends = Convert.ToDouble(dwWT["WTBFDZ" + YNi].ToString());
                    if (ends == 0)
                        break;
                    dwData = TBMain.Rows.Add();
                    TMV.SetUpNewRow(ref dwData);

                    dwData["BAGART"] = BEAMART.Substring(1, 20);
                    dwData["BAGVAR"] = BEAMART.Substring(21, 2);
                    dwData["BAGFARBE"] = BEAMART.Substring(23, 10);
                    dwData["BAGPOS"] = Helper.Right("00" + YNi, 2);
                    dwData["BAGFDZ"] = ends;
                    TMV.InsertBASTGA(dwData, app.company);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public bool Validate()
        {
            bool IsValidate = true;
            double ends = 0;
            double wastage = 0;
            double beamlg = FAKTOR;
            double K = 0;
            double qty = 0;
            foreach (DataRow row in TBMain.Rows)
            {
                ART = row["BAGGART"].ToString() + row["BAGGVAR"].ToString() + row["BAGGFARBE"].ToString();
                PM["CART"] = ART;
                SResult = TMV.VGS.LOAD(PM);
                foreach (DataRow dwGS in SResult.Tables[0].Rows)
                {
                    string ctyp = dwGS["GSRECHNUM"].ToString().Trim();
                    K = Convert.ToDouble(dwGS["GSRECHNR"].ToString()) * 0.0001;//每克重纱的米数
                    K = UMHelper.GPKM(ctyp, K,app.company);
                }

                ends = Convert.ToDouble(row["BAGFDZ"].ToString());
                wastage = Convert.ToDouble(row["BAGABF"].ToString()) * 0.1 * 0.01;
                qty = Helper.RoundX(beamlg * ends * K / 1000, 100);
                row["BAGKGNET"] = qty * 0.001 * 1000;
                qty = Helper.RoundX((1 + wastage) * beamlg * ends * K / 1000, 10);
                row["BAGKGBRUT"] = qty * 0.001 * 1000;
            }
            WEIGHT = Helper.RoundX(TMV.SUM1 / 100, 1) * 100;
            return IsValidate;
        }
        public virtual void OnYARNChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (YARN.Length < 33)
                return;
            TMV.BAGGART = YARN.Substring(1, 20);
            TMV.BAGGVAR = YARN.Substring(21, 3);
            TMV.BAGGFARBE = YARN.Substring(24, 10);
            TMV.BAGFDZ = WARPENDS;
            TMV.BAGABF = 50; 
        }
    }
}
